package Limit.Game.MarkSix;


import java.sql.Date;
import java.util.Collection;
import java.util.Timer;
import java.util.TimerTask;

import l1j.server.server.GeneralThreadPool;
import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_BlueMessage;
import l1j.taki.GetNowTime;

public class MarkSix extends TimerTask{
	
	private Timer _timeHandler = new Timer(true);	

	private int _currentTime = (((GetNowTime.GetNowHour()*60) + GetNowTime.GetNowMinute())*60)+ GetNowTime.GetNowSecond();
	
	private int _startTime = (21*60+30)*60;//生成下一期項目及開始接受投注：21:30
	
	private int _markEndTime = (21*60+05)*60;//轉移所有中獎/不中獎彩票 21:05
	
	private int _publicTime = (21*60+01)*60;//公佈開獎結果時間點：21:00
	
	private int _createNumTime = (20*60+32)*60; //生成7個隨機號碼及估計派彩 20:32
	
	private int _removeNpcTime = (20*60+30)*60; //關閉npc投注 20:30
	
	private int _ticketsCount;
	
	private int[][] _ticketsInfo;
	
	private String[][] _ticketsNumInfo;
	
	private Date[] _dateTime;
	
	public static boolean _canBuyTime = true;
	
	private static MarkSix _instance;
	
	public static MarkSix getStart() {
		if (_instance == null) {
			_instance = new MarkSix();
		}
		return _instance;
	}
	
	/*public void startMarkSix(){
		MarkSix.getStart();
	}*/
	
	MarkSix() {
		// 開始執行此時間軸
		_timeHandler.schedule(this, 1000, 1000);
		// 交由線程工廠 處理
		GeneralThreadPool.getInstance().execute(this);
	}
	
	public void run() {
		_currentTime ++;
		
		if (_currentTime >= _removeNpcTime && _currentTime < _startTime-60)
		{
			_canBuyTime = false;
		}
		
		if (_currentTime == _startTime){
			MarkSix_Data_Table geneTodayTotal = new MarkSix_Data_Table();
			geneTodayTotal.insertTodayMarkSixTotal();
			_canBuyTime = true;
		}
		
		if (_currentTime == _createNumTime){
			MarkSix_Data_Table data = new MarkSix_Data_Table();
			String[] createNum = checkIncome();
			data.updateMarkSixTotalNum(createNum);
		}
		if (_currentTime == _markEndTime){
			MarkSix_Tickets_Table tickets = new MarkSix_Tickets_Table();
			MarkSix_StrikeTickets_Table strike = new MarkSix_StrikeTickets_Table();
			MarkSix_RubbishTickets_Table rubbish = new MarkSix_RubbishTickets_Table();
			MarkSix_Data_Table data = new MarkSix_Data_Table();
			
			rubbish.deleteRubbishTickets();
			
			int numType;
			int wager;
			int char_id;

			String resultNum[] = data.loadMarkSixNum();

			// 核對所有彩票
			for (int k = 0; k < _ticketsCount; k++) {
				numType = _ticketsInfo[k][0];
				wager = _ticketsInfo[k][1];
				char_id = _ticketsInfo[k][2];
				
				MarkSixCheck check = new MarkSixCheck(resultNum,_ticketsNumInfo[k],numType,wager);
				
				if (check.CheckNum()){
					strike.saveStrikeTickets(char_id, numType, wager, check.bonus, _ticketsNumInfo[k], _dateTime[k]);
				}else{
					rubbish.saveRubbishTickets(char_id, numType, wager, _ticketsNumInfo[k], _dateTime[k]);
				}
			}			
			tickets.deleteTickets();			
		}
			
		if (_currentTime == _publicTime){
			MarkSix_Data_Table data = new MarkSix_Data_Table();
			String ss[] = data.loadMarkSixNum();
			for(int i=0;i<ss.length;i++){
				switch (i){
				case 0:
					NewsToAll("\\f3第一個開獎號碼是”"+ss[i]+"”");
					L1World.getInstance().broadcastServerMessage("\\f3第一個開獎號碼是”"+ss[i]+"”");
					break;
				case 1:
					NewsToAll("\\f3第二個開獎號碼是”"+ss[i]+"”");
					L1World.getInstance().broadcastServerMessage("\\f3第二個開獎號碼是”"+ss[i]+"”");
					break;
				case 2:
					NewsToAll("\\f3第三個開獎號碼是”"+ss[i]+"”");
					L1World.getInstance().broadcastServerMessage("\\f3第三個開獎號碼是”"+ss[i]+"”");
					break;
				case 3:
					NewsToAll("\\f3第四個開獎號碼是”"+ss[i]+"”");
					L1World.getInstance().broadcastServerMessage("\\f3第四個開獎號碼是”"+ss[i]+"”");
					break;
				case 4:
					NewsToAll("\\f3第五個開獎號碼是”"+ss[i]+"”");
					L1World.getInstance().broadcastServerMessage("\\f3第五個開獎號碼是”"+ss[i]+"”");
					break;
				case 5:
					NewsToAll("\\f3第六個開獎號碼是”"+ss[i]+"”");
					L1World.getInstance().broadcastServerMessage("\\f3第六個開獎號碼是”"+ss[i]+"”");
					break;
				case 6:
					NewsToAll("\\f3特別號碼是”"+ss[i]+"”");
					L1World.getInstance().broadcastServerMessage("\\f3特別號碼是”"+ss[i]+"”");
					break;
				default:
					break;
				}
				try {
					Thread.sleep(12000);
				}catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/** 全體公告　\\f3紅色 
	 * @author 0724*/
	private void NewsToAll( String string ) {
		Collection <L1PcInstance> AllPc = L1World.getInstance().getAllPlayers(); 
		for ( L1PcInstance pc : AllPc ){
		pc.sendPackets(new S_BlueMessage(166,string)); 
		}
	}
	
	private String[] checkIncome(){
		MarkSixNum sevenNum = new MarkSixNum();
		String[] resultNum = sevenNum.get();
		
		MarkSix_Data_Table data = new MarkSix_Data_Table();	
		MarkSix_Tickets_Table tickets = new MarkSix_Tickets_Table();
		
		tickets.loadTickets();					
		int numType;
		int wager;
		int totalReward = 0;
		int income = 0;
		int totalWager = 0;

		_ticketsCount = tickets.getTotalTicketsCount();
		_ticketsInfo = tickets.getTicksIntArray();
		_ticketsNumInfo = tickets.getTicketsStringArray();
		_dateTime = tickets.getDateTime();

		for (int k = 0; k < _ticketsCount; k++) {
			numType = _ticketsInfo[k][0];
			wager = _ticketsInfo[k][1];
			
			MarkSixCheck check = new MarkSixCheck(resultNum,_ticketsNumInfo[k],numType,wager);
			
			if (check.CheckNum()){
				totalReward += check.bonus;
			}
			totalWager += wager;
		}
		income = (totalWager - totalReward);
		if ( totalReward > totalWager * 0.9)
			return checkIncome();
		data.updateMarkSixTotalReward(totalReward);
		data.saveMarkSixData(income);
		
		return resultNum;
	}
}
